layui.define(['element','form','layer','table','slider'], function(exports){
  let $ = layui.jquery,
    form = layui.form,
    table = layui.table,
    layer = layui.layer,
    slider = layui.slider;
  let obj = {
    find:function (tb_data,tb_name,url) {  //表单数据 表格名字 查表
      let obj = tb_data.field;
      for (let i in obj) {
        if (obj[i] == "") {
          delete obj[i]
        }
      }
      console.log(obj)
      table.reload(tb_name, {
        url: url//发送请求
        , where: obj
        , done: function (res, curr, count) {
          this.where = {};
        }
      });
    },

    setSlider(elmName,min ,max,step,valueName){   //滑块名字 最小值 最大值 间隔 滑块位置量
      let slid =  slider.render({
        elem: elmName  //绑定元素
        ,min: min
        ,max: max
        ,step: step
        ,input:true
        ,change: function(value){

          valueName = value;
        }

      });
      return valueName;
    },

    showLayer : function (obj) {
      let str = obj.name;
      let width = obj.width?obj.width:'600px';
      let height = obj.height?obj.height:'300px';
      let title = obj.title?obj.title:false;
      layer.open({
        type: 1
        , name: "yea"
        , title: title //不显示标题栏
        , closeBtn: 1
        , area: [width, height]
        , shade: 0
        , id: 'LAY_layuipro' //设定一个id，防止重复弹出
        //,btn: ['确认添加', '取消']
        , btnAlign: 'c'
        , moveType: 1 //拖拽模式，0或者1
        , content: $(str)
        //,maxmin: true
        , success: function (layero) {
        }
      });
    },
    setSelect: function (url,tagId,idIsName,levelPlace) {            //查表 列表id id是否要等于value 选中哪一行
      $.ajax({
        url:url,
        type:'POST',
        dataType:'json',
        success:function (data) {
          $("#" + tagId).empty();
          $("#" + tagId).append('<option value="">请选择</option>');
          if(data!= null && data.length != 0) {
            $.each(data,function(i,place) {
              let name = Object.values(place)[0];
              let id = Object.values(place)[1];

              if(idIsName){
                id = name;
              }

              //$("#" + tagId).append('<option value="'+id+'">'+ name+'</option>')
              if(levelPlace && levelPlace == id) {
                $("#" + tagId).append('<option selected value="'+id+'">'+name+'</option>')
              }else {
                $("#" + tagId).append('<option value="'+id+'">'+name+'</option>')
              }
            })
          }
          form.render('select'); //刷新select选择框渲染
        }
      })
    },

    postMsg: function (msg, callback) {
      $.ajax({
        url: msg.url,
        type: 'POST',
        dataType: 'json',
        data: msg.data,
        success: callback
      });
    },
  }

  //输出模块
  exports('common', obj);
});
